import { _decorator, Color, Component, EventTouch, Label, Node, Sprite, UITransform, Vec2, Vec3 } from 'cc';
import { XUtils } from './comm/XUtils';
import { GameCtrl } from './GameCtrl';
import { CommTweens } from './comm/CommTweens';
const { ccclass, property } = _decorator;

@ccclass('Cell')
export class Cell extends Component {
    public index: number;

    private isFilp: boolean;
    get IsFilp(){
        return this.isFilp;
    }
    set IsFilp(value: boolean){
        this.isFilp = value;
        if(value){
            CommTweens.fadeOut(this.spFilp.node).start();
        }
    }

    private isHide: boolean;
    get IsHide(){
        return this.isHide;
    }
    set IsHide(value: boolean){
        this.isHide = value;
        if(value){
            this.spHide.node.active = true;
        }else{
            this.spHide.node.active = false;
        }
    }

    private isMine: boolean;
    get IsMine(){
        return this.isMine;
    }
    set IsMine(value: boolean){
        this.isMine = value;
    }

    private mineNum: number;
    get MineNum(){
        return this.mineNum;
    }
    set MineNum(value: number){
        this.mineNum = value;
    }


    @property(Sprite)
    spFilp: Sprite;
    @property(Sprite)
    spMine: Sprite;
    @property(Sprite)
    spBom: Sprite;
    @property(Sprite)
    spHide: Sprite;
    
    public onLoad(): void {
        // XUtils.bindClick(this.node, this.Click, this);
        // this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        // this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        // this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        // this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    }

    public onDestroy(): void {
        // this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        // this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        // this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        // this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    }

    private _touchTimer: number = 0;
    private _isLongPress: boolean = false;
    private _longPressThreshold: number = 0.1; // 长按判定时间（秒）
    private _touchEvent: EventTouch | null = null;

    

    private onTouchStart(event: EventTouch) {
        const touches = event.getTouches();
        if(touches.length == 2){
            GameCtrl.Instance._isCellON = false;
            GameCtrl.Instance._isCellScaleOn = true;
            GameCtrl.Instance._pointer1 = touches[0].getUILocation();
            GameCtrl.Instance._pointer2 = touches[1].getUILocation();
            GameCtrl.Instance._initDistance = GameCtrl.Instance._pointer1.subtract(GameCtrl.Instance._pointer2).length();
            GameCtrl.Instance._initOrthoHeight = GameCtrl.Instance.gameCamera.orthoHeight;
        }else{
            this._isLongPress = false;
            this._touchTimer = 0;
            this._touchEvent = event;
            this.schedule(this.checkLongPress, 0.01);
        }
    }

    private onTouchMove(event: EventTouch) {
        const touches = event.getAllTouches();
        if(touches.length == 2){
            GameCtrl.Instance.currPointer1 = touches[0].getUILocation();
            GameCtrl.Instance.currPointer2 = touches[1].getUILocation();
        }else{
            const pos2 = event.getUILocation();
            GameCtrl.Instance._lastTouchPos.set(pos2.x, pos2.y, 0);
        }
    }

    private checkLongPress() {
        this._touchTimer += 0.01;
        if (this._touchTimer >= this._longPressThreshold && !this._isLongPress) {
             GameCtrl.Instance._isCellScaleOn = false;
            this._isLongPress = true;
            this.unschedule(this.checkLongPress);
            const pos2 = this._touchEvent.getUILocation();
            GameCtrl.Instance._lastTouchPos.set(pos2.x, pos2.y, 0);
            const parent = GameCtrl.Instance.gameArea.parent!;
            const localPos = parent.getComponent(UITransform)!.convertToNodeSpaceAR(new Vec3(pos2.x, pos2.y, 0));
            GameCtrl.Instance._dragOffset = GameCtrl.Instance.gameArea.position.clone().subtract(localPos);
            GameCtrl.Instance._isCellON = true;
        }
    }

    private onTouchEnd(event: EventTouch) {
        this.unschedule(this.checkLongPress);
        if (!this._isLongPress) {
            // 普通点击
            this.Click();
        }
        this._touchEvent = null;
        GameCtrl.Instance._isCellON = false;
        GameCtrl.Instance._isCellScaleOn = false;
    }

    private onTouchCancel(event: EventTouch) {
        this.unschedule(this.checkLongPress);
        this._touchEvent = null;
        GameCtrl.Instance._isCellON = false;
        GameCtrl.Instance._isCellScaleOn = false;
    }
    
    public Click(){
        //GameCtrl.Instance.handleFirstClick(this.index);

        if(GameCtrl.Instance.isLeft){
            GameCtrl.Instance.handleLeftClick(this);
        }else{
            GameCtrl.Instance.handleRightClick(this);
        }
    }

    public showMine(){
        this.spMine.node.active = true;
    }
    
    public showBow(){
        this.spMine.node.active = true;
        this.spBom.node.active = true;
    }



    public resetCell(){
        this.IsFilp = false;
        this.IsHide = false;
        //this.IsMine = false;
        this.spMine.node.active = false;
        this.spBom.node.active = false;
    }
}


